import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Provide with the gate used by the passengers, 
 * the number of passengers at the gate as well as the time of arrival of the  first pasenger at immigration
 * local directory is   home/hnikoue/Documents/Sydney/Data/Results/Filtered
 * files: 	count_rateAtImmi1_112.csv	January
 * 			count_rateAtImmi2_112.csv	February	
 * 			count_rateAtImmi4_112.csv	April
 * 			count_rateAtImmi5_112.csv	May
 * 			count_rateAtImmi6_112.csv	June
 * 			count_rateAtImmi7_112.csv	July
 * 			count_rateAtImmi8_112.csv	August
 * 			count_rateAtImmi9_112.csv	Septemeber
 * The structure of these files is described in count_rate_structure.ods
 */

/**
 * @author hnikoue
 *
 */
public class PassengersGateTime {
	private  String DIRECTORY = "/home/hnikoue/Documents/Sydney/Data/Results/Filtered"; 
	BufferedReader buf;
	private ArrayList<Flight> processedFlights = new ArrayList<Flight>();
	protected int pos = 1;
	
	/**
	 * 
	 * @param date
	 */
	public PassengersGateTime(LocalArrTime date){
			if(!findFile(date))
				processedFlights = null;
	}
	/**
	 * 
	 * @return
	 */
	public ArrayList<Flight> flightHistory(){
		return processedFlights;
	}
	/**
	 * Create an array with all entries in a file that start at the same date.
	 * First need to find a file count_rate with matching month
	 * Then match the day given with entries in the file 
	 * @param dateName
	 * @return
	 */
	public boolean findFile(LocalArrTime date){
		int month,year;
		String monthOfInterest;//month on which to focus the analysis
		String fileName,fileNameShort;//complete and short file name
		boolean isFound = false;
		/*
		 * build a string in the format m_YYY
		 */
		
		month = date.getMonth();
		year = date.getYear()-1900;//go from BC to post 1900
		monthOfInterest = Integer.toString(month)+"_"+Integer.toString(year);
		/*
		 * See if the entries in the current directory 
		 */
		fileNameShort = "count_rateAtImmi"+monthOfInterest+".csv";//filename to open without directory information
		fileName = DIRECTORY+"/"+fileNameShort;
		try {
			buf = new BufferedReader(new FileReader(fileName));
			isFound = findDateInFile(date);
			
		} catch (IOException e1) {
			e1.printStackTrace();
		}finally{
			try{
				if (buf != null)
					buf.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			
		}
		
		return isFound;
		
	}
	/**
	 * Go through the buf file in search of the specific date in question
	 * @param date
	 * @return
	 */
	private boolean findDateInFile(LocalArrTime date) {
		boolean dateFound= false;
		String lineList[] = new String[8];
		String dateParts[] = new String[3];//first part is the day second part the time, third part
		int passengerCount, timeInS;
		double rate;
		String flightID,gate,desks;
		String sCurrentLine ;
		String referenceDate; //reference datum to use to compare to a file line
		LocalArrTime timeArr1stPAXAtImmi;
		Flight landedFlight;
		
		referenceDate = Integer.toString(date.getDay())+"/"+String.format("%02d",date.getMonth())+"/"+Integer.toString(date.getYear());
		try {
			buf.readLine();
			while((sCurrentLine = buf.readLine()) != null){
			/*
			 * Get the line and split it to obtain the dateString
			 */
				lineList = sCurrentLine.split(",");//split the strings by comma
				if(lineList.length<2)
					break;//you are getting garbage from that point
				if(lineList[2].contentEquals("A")){//only consider arrivals for now
					dateParts = lineList[0].split(" ");// split by space
					if(dateParts[0].compareTo(referenceDate)==0){//the two strings are identical
						if(Integer.parseInt(lineList[7])>=50){//only gates 50-63 are contained in all files
							dateFound = true;
							passengerCount = Integer.parseInt(lineList[4]);
							gate = "gate"+lineList[7];
							flightID = lineList[1];
							timeInS = (int) Double.parseDouble(lineList[3]);
							rate = Double.parseDouble(lineList[5]);
							desks = lineList[6];
							timeArr1stPAXAtImmi = new LocalArrTime(lineList[0]);
							landedFlight = new Flight(flightID,gate,passengerCount,timeArr1stPAXAtImmi,desks,timeInS,rate); 
							landedFlight.setTagID(pos++);
							processedFlights.add(landedFlight);
						}
					}
				}
		}
			
			
		} catch (IOException e) {

			e.printStackTrace();
		}
		
		
		return dateFound;
	}

}
